<?php
namespace app\common\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\Db;

class PaymentReport extends Command
{
    protected function configure()
    {
        $this->setName('payment_stats')
            ->addArgument('action', Argument::OPTIONAL, "方法名称")
            ->addOption('start_date', null, Option::VALUE_REQUIRED, '开始日期')
            ->addOption('end_date', null, Option::VALUE_REQUIRED, '结束日期')
            ->setDescription('支付号数据汇总');
    }

    protected function execute(Input $input, Output $output)
    {

        $action = $input->getArgument('action');

        if (method_exists($this, $action)) {

            $start_date = $input->hasOption('start_date') ? $input->getOption('start_date') : date('Y-m-d', strtotime('-1days'));
            $end_date = $input->hasOption('end_date') ? $input->getOption('end_date') : date('Y-m-d', strtotime('-1days'));

            $this->start_date = $input->hasOption('start_date') ? $input->getOption('start_date') : '';
            $this->end_date = $input->hasOption('end_date') ? $input->getOption('end_date') : '';

            $this->$action($start_date, $end_date);
        }

    }

    /*支付号日明细统计*/
    public function daily($start_date,$end_date)
    {
        if ($start_date == $end_date && $end_date != date('Y-m-d') ) {
            $start_date = date('Y-m-d', strtotime('-1days'));
            $end_date = date('Y-m-d', strtotime('-1days'));
        }


        $date_rang = getDateByRange($start_date, $end_date);
        $payments = Db::table('quick_payment_number')->field('id')->select();

        foreach ($date_rang as $date) {

            foreach ($payments as $pay) {

                $recharged_num = Db::table('pay_order')->where('chargefrom',$pay['id'])
                                ->where('create_date',$date)->where('status',2)
                                ->count('id');

                $unpaid_num = Db::table('pay_order')->where('chargefrom',$pay['id'])
                                ->where('create_date',$date)->where('status',1)
                                ->count('id');

                $recharged_num_kl = Db::table('pay_order_kl_backups')->where('chargefrom',$pay['id'])
                                ->where('create_date',$date)->where('status',2)
                                ->count('id');

                $unpaid_num_kl = Db::table('pay_order_kl_backups')->where('chargefrom',$pay['id'])
                                    ->where('create_date',$date)->where('status',1)
                                    ->count('id');

                $money = Db::table('pay_order')->where('chargefrom',$pay['id'])
                            ->where('create_date',$date)->where('status',2)
                            ->sum('total_fee');

                $money_kl = Db::table('pay_order_kl_backups')->where('chargefrom',$pay['id'])
                            ->where('create_date',$date)->where('status',2)
                            ->sum('total_fee');


                $data['create_date']    = $date;
                $data['recharged_num']  = $recharged_num+$recharged_num_kl;
                $data['unpaid_num']     = $unpaid_num+$unpaid_num_kl;
                $data['chargefrom']     = $pay['id'];
                $data['money']          = $money+$money_kl;

                if($pay['id'] == 4 && $date == '2024-03-31')
                {
                    echo $money.'---'.$money_kl;
                    exit;
                }

                $record = Db::table('rp_payment_number_daily')->where('create_date',$date)
                            ->where('chargefrom',$pay['id'])->field('id')->findOrEmpty();

                if($record)
                {
                    Db::table('rp_payment_number_daily')->where('id',$record['id'])->update($data);
                }else{
                    Db::table('rp_payment_number_daily')->insert($data);
                }
            }

        }


    }


}